import 'dart:core';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../base/MyScaffold.dart';
import '../../res.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../config/Constant.dart';
import '../../internationalization/json.dart';
import '../../model/LaguageEntity.dart';
import '../home/HomeTabBar.dart';

class Laguage extends StatefulWidget {
  const Laguage({Key? key}) : super(key: key);

  @override
  State<Laguage> createState() => _LaguageState();
}

class _LaguageState extends State<Laguage> {
  List<LaguageEntity> list = [];

  @override
  void initState() {
    // TODO: implement initState
    // LaguageEntity chinese =
    //     LaguageEntity("chinese".tr, const Locale('zh', 'CN'));
    // LaguageEntity english =
    //     LaguageEntity("english".tr, const Locale('en', 'US'));
    Languages.forEach((key, value) {
      Map<String, String> map = value;
      var cname = map["cname"];
      var dir = map["dir"];
      var lang = map["lang"];
      var code = map["code"];
      var name = map["name"];
      var entity = LaguageEntity(cname, dir, lang, code, name);
      list.add(entity);
    });
    print(Get.locale?.languageCode);
    print(Get.locale?.countryCode);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      title: "language".tr,
      body: ListView.builder(
        itemBuilder: (c, index) {
          LaguageEntity entity = list[index];
          return InkWell(
            onTap: () async {
              var locale = Locale(entity.lang!, entity.code!);
              EasyLoading.show(status: "loading");
              await Get.updateLocale(locale).then((value) {
                Future.delayed(const Duration(milliseconds: 1000), () {
                  EasyLoading.dismiss();
                  Get.offAll(const HomeTabBar());
                  SpUtil.putString(Constant.lanuage, entity.lang!);
                  SpUtil.putString(Constant.cname, entity.cname!);
                  SpUtil.putString(Constant.code, entity.code!);
                });
              });
            },
            child: Container(
              height: 60,
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                children: [
                  TextView(entity.cname),
                  const Spacer(),
                  Visibility(
                      visible: Get.locale?.countryCode?.toLowerCase() ==
                              entity.dir?.toLowerCase()
                          ? true
                          : false,
                      child: const MyLocalImage(
                        Res.list_icon_select,
                        width: 20,
                        height: 20,
                      ))
                ],
              ),
            ),
          );
        },
        itemCount: list.length,
      ),
    );
  }
}
